In [ ]:
%%javascript
// ----- BASICS -----
// Count number of documents in collection
db.getCollection('facebook').count()
db.getCollection('twitter').count()
// find and findOne
db.getCollection('twitter').find({}).limit(5)
db.getCollection('twitter').findOne({})
// ----- FILTER FOR VIDEO -----
// What tweets have videos?
db.getCollection('twitter').find(
{ 'tweet.extended_entities': {$exists : true},
'tweet.extended_entities.media.type' : 'video'})
// Count tweets with video
db.getCollection('twitter').find(
{ 'tweet.extended_entities': {$exists : true},
'tweet.extended_entities.media.type' : 'video'}).count()
// ----- FILTER RESPONSE -----
// Filter tweets and return specific fields
db.getCollection('twitter').find(
{
// Filter tweets that have video
'tweet.extended_entities': {$exists : true},
'tweet.extended_entities.media.type' : 'video'
},
{
// What to keep
'tweet.text':1,
'tweet.id' : 1,
'tweet.retweeted':1,
'tweet.retweet_count': 1,
'tweet.hashtags.text':1,
'tweet.geo':1,
'tweet.extended_entities.media.expanded_url' : 1,
'tweet.extended_entities.media.display_url' : 1,
'tweet.extended_entities.media.source_user_id_str' : 1,
'tweet.extended_entities.media.video_info' : 1,
'tweet.extended_entities.media.id_str' : 1,
'tweet.extended_entities.media.id' : 1,
'tweet.extended_entities.media.media_url' : 1,
'created_at' :1
}
)
In [ ]:
%%javascript
// ----- SAVE RESULT TO NEW COLLECTION -----
var query = db.getCollection('twitter').find(
{
// Filter tweets that have video
'tweet.extended_entities': {$exists : true},
'tweet.extended_entities.media.type' : 'video'
},
{
// What to keep
'tweet.text':1,
'tweet.id' : 1,
'tweet.retweeted':1,
'tweet.retweet_count': 1,
'tweet.hashtags.text':1,
'tweet.geo':1,
'tweet.extended_entities.media.expanded_url' : 1,
'tweet.extended_entities.media.display_url' : 1,
'tweet.extended_entities.media.source_user_id_str' : 1,
'tweet.extended_entities.media.video_info' : 1,
'tweet.extended_entities.media.id_str' : 1,
'tweet.extended_entities.media.id' : 1,
'tweet.extended_entities.media.media_url' : 1,
'created_at' :1
}
).toArray()
db.results.insert(query)
In [ ]:
db.results.drop()
In [ ]:
%%javascript
// Create connection
var conn = new Mongo();
db = conn.getDB("test");
// Define query (same as robomongo)
var query = db.getCollection('twitter').find(
{
// Filter tweets that have video
'tweet.extended_entities': {$exists : true},
'tweet.extended_entities.media.type' : 'video'
},
{
// What to keep
'tweet.text':1,
'tweet.id' : 1,
'tweet.retweeted':1,
'tweet.retweet_count': 1,
'tweet.hashtags.text':1,
'tweet.geo':1,
'tweet.extended_entities.media.expanded_url' : 1,
'tweet.extended_entities.media.display_url' : 1,
'tweet.extended_entities.media.source_user_id_str' : 1,
'tweet.extended_entities.media.video_info' : 1,
'tweet.extended_entities.media.id_str' : 1,
'tweet.extended_entities.media.id' : 1,
'tweet.extended_entities.media.media_url' : 1,
'created_at' :1
}
).toArray()
// Save query to 'results' collection
db.results.insert(query)
In [ ]:
mongo filter_example.js